#!/bin/bash

set -eu -o pipefail

[[ $EUID != 0 ]] && printf "Please run as root\n" && exit 1

[[ -d /usr/src/linux ]] && cd /usr/src/linux
kver=$(make kernelversion)

# Put boot, config, system-map and initramfs on boot dir
if [[ -e /boot/vmlinuz-$kver ]] ; then
	mv /boot/vmlinuz-$kver /boot/vmlinuz-$kver.old
	mv /boot/config-$kver /boot/config-$kver.old
	mv /boot/System.map-$kver /boot/System.map-$kver.old
fi

# Store the kernel, system-map and initramfs in boot directory
cp ./arch/$(uname -m)/boot/bzImage /boot/vmlinuz-$kver
cp .config /boot/config-$kver
cp System.map /boot/System.map-$kver
printf "Making initramfs using dracut\n"
dracut -f -H --kver=$kver &>/dev/null
printf "Copied kernel, config, system-map and initramfs to /boot\n"

# Install kernel and initramfs
cp /boot/vmlinuz-$kver /boot/efi/boot/bootx64.efi
cp /boot/initramfs-$kver.img /boot/efi/boot/initramfs.img
printf "Copied kernel and initramfs to /boot/efi\n"
